//
//  SendAdviceToParentsViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2018/1/9.
//  Copyright © 2018年 史宵宵. All rights reserved.
//

#import "SendAdviceToParentsViewController.h"
#import "XHStarRateView.h"

@interface SendAdviceToParentsViewController ()<UITextViewDelegate,XHStarRateViewDelegate>
@property (nonatomic,weak) JSTextView *releaseContentTextView;
@property (nonatomic,assign) BOOL isEdit;
@end

@implementation SendAdviceToParentsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.titleStr;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (self.sendAdviceToParentsType == KParentsSendAdviceToParentsType) {
        [self creatFootView];
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightBarButtonItemWithTitle:@"编辑" target:self selector:@selector(editAdvice)];
    }
}
- (void)editAdvice{
    self.isEdit = YES;
    [self creatFootView];
    [self.tableView reloadData];
}
#pragma mark - creatFootView
- (void)creatFootView{
    UIView *footView = [[UIView alloc]init];
    footView.backgroundColor = [UIColor whiteColor];
    footView.frame = CGRectMake(0, 0, kScreenW, 150);
    CompleteButton *completeButton = [[CompleteButton alloc]initWithFrame:CGRectMake(20, 60, kScreenW - 40, 50) bgColor:KBACKNAVIGATIONCOLOR tColor:[UIColor whiteColor] text:@"发给孩子"];
    completeButton.userInteractionEnabled = self.isEdit;
    if (!self.isEdit) {
        completeButton.backgroundColor =  [UIColor lightGrayColor];
    }else{
        [completeButton addTarget:self action:@selector(sendAdviceToParents) forControlEvents:UIControlEventTouchUpInside];
    }
    [footView addSubview:completeButton];
    self.tableView.tableFooterView = footView;
    
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section) {
        return self.scoreArray.count;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section) {
        static NSString *identifier = @"cell1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        //        if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        //        }

        UILabel *desLabel = [[UILabel alloc]init];
//        desLabel.text = self.coreArray[scoreInteger-1];
        desLabel.text = self.scoreArray[indexPath.row][@"content"];
        desLabel.frame = CGRectMake(20, 15, 80, 30);
        desLabel.textColor = [UIColor whiteColor];
        desLabel.backgroundColor = KBACKNAVIGATIONCOLOR;
        desLabel.textAlignment = NSTextAlignmentCenter;
        desLabel.clipsToBounds = YES;
        desLabel.font = [UIFont systemFontOfSize:14];
        desLabel.layer.cornerRadius = 15;
        [cell.contentView addSubview:desLabel];
//        self.desLabel = desLabel;
        
        NSString *scoreStr = self.scoreArray[indexPath.row][@"value"];
        scoreStr = strIsEmpty(scoreStr)? @"5": scoreStr;
//        NSInteger scoreInteger = (strIsEmpty(scoreStr) || [scoreStr isEqualToString:@"0"])? 5: [scoreStr integerValue];
        XHStarRateView *starRateView = [[XHStarRateView alloc]init];
        starRateView.isMove = self.sendAdviceToParentsType == KParentsSendAdviceToParentsType ?self.isEdit:NO;
        starRateView.rateStyle = WholeStar;
        starRateView.tag = 1000+indexPath.row;
        starRateView.frame = CGRectMake(kScreenW - 170, 17, 150, 25);
        [starRateView setStarScore:self.scoreArray[indexPath.row][@"value"]];
        starRateView.delegate = self;
        [cell.contentView addSubview:starRateView];
//        self.starView = starRateView;
        
        UILabel *scoreLabel = [[UILabel alloc]init];
        scoreLabel.frame = CGRectMake(20, 55, kScreenW - 20, [Global getSizeOfString:self.scoreArray[indexPath.row][@"explain"] maxWidth:kScreenW - 40 maxHeight:MAXFLOAT withFontSize:14].height);
        scoreLabel.textAlignment = NSTextAlignmentLeft;
        scoreLabel.font = [UIFont systemFontOfSize:14];
        scoreLabel.text = self.scoreArray[indexPath.row][@"explain"];
        scoreLabel.numberOfLines = 0;
        scoreLabel.textColor =  KHEADERTITLECOLOR;
        [cell.contentView addSubview:scoreLabel];
//        self.scoreLabel = scoreLabel;
        
        return cell;
    }
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell0"];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (self.sendAdviceToParentsType != KParentsSendAdviceToParentsType) {
        UILabel *label = [[UILabel alloc]init];
        label.frame = CGRectMake(10, 10, kScreenW - 20, [Global getSizeOfString:self.sureContentStr maxWidth:kScreenW - 20 maxHeight:200 withFontSize:16].height + 20);
        label.textColor = KBACKNAVIGATIONCOLOR;
        label.numberOfLines = 0;
        label.font = [UIFont systemFontOfSize:16];
        label.text = self.sureContentStr;
        [cell.contentView addSubview:label];
    }else{
        UILabel *label = [[UILabel alloc]init];
        label.frame = CGRectMake(10, 10, kScreenW - 20, 50);
        label.textColor = KTEXTMAINCOLOR;
        label.numberOfLines = 2;
        label.text = @"写下您对孩子的这个月的点评和下个月进步的具体希望:";
        [cell.contentView addSubview:label];
        
        JSTextView *releaseContentTextView = [[JSTextView alloc] initWithFrame:CGRectMake(10, 70, kScreenW - 20, 100)];
        releaseContentTextView.backgroundColor = KBACKGROUNDCOLOR;
        releaseContentTextView.font = [UIFont systemFontOfSize:16];
        releaseContentTextView.textColor = KTEXTMAINCOLOR;
        releaseContentTextView.delegate = self;
        releaseContentTextView.autocorrectionType = UITextAutocorrectionTypeNo;  //取消纠错
        releaseContentTextView.autocapitalizationType = UITextAutocapitalizationTypeNone;  //取消首字母大写
        releaseContentTextView.returnKeyType = UIReturnKeyDefault;  //return改成done
        releaseContentTextView.myPlaceholder = @"这里是写总结......";
        [cell.contentView addSubview:releaseContentTextView];
        releaseContentTextView.text = strIsEmpty(self.sureContentStr)?@"":self.sureContentStr;
        releaseContentTextView.userInteractionEnabled = self.isEdit;
        self.releaseContentTextView = releaseContentTextView;
    }
    
    return cell;

    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section) {
        return 60 + [Global getSizeOfString:self.scoreArray[indexPath.row][@"explain"] maxWidth:kScreenW - 40 maxHeight:MAXFLOAT withFontSize:14].height + 10;
    }
    return self.sendAdviceToParentsType == KParentsSendAdviceToParentsType ? 200 : [Global getSizeOfString:self.sureContentStr maxWidth:kScreenW - 20 maxHeight:200 withFontSize:16].height + 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    if (!section && self.sendAdviceToParentsType == KParentsSendAdviceToParentsType) {
        view.frame = CGRectMake(0, 0, kScreenW, 0.01);
        return view;
    }
    view.frame = CGRectMake(0, 0, kScreenW, 44);
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 12, kScreenW - 20, 20)];
    label.text = self.sendAdviceToParentsType == KParentsSendAdviceToParentsType? @"您对孩子本月表现评分":section?@"本月表现评分": @"看一下：家长看到我的进步";
    label.textColor = KTEXTMAINCOLOR;
    label.font = [UIFont systemFontOfSize:16];
    [view addSubview:label];
    
    UILabel *sepLabel = [[UILabel alloc]init];
    sepLabel.backgroundColor = KBORDERCOLOR;
    sepLabel.frame = CGRectMake(0, 43, kScreenW, 1);
    [view addSubview:sepLabel];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (!section && self.sendAdviceToParentsType == KParentsSendAdviceToParentsType) {
        return 0.01f;
    }
    return 44.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 5);
//    view.backgroundColor = KBACKGROUNDCOLOR;
    if (section) {
        view.frame = CGRectMake(0, 0, kScreenW, 0.01);
    }
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section) {
        return 0.01f;
    }
    return 5.01f;
}
#pragma mark - 选择星星
-(void)starRateView:(XHStarRateView *)starRateView currentScore:(CGFloat)currentScore{
//    NSLog(@"%d----  %f",starRateView.tag- 1000,currentScore);
    NSInteger index = starRateView.tag- 1000;
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[NSString stringWithFormat:@"%f",currentScore] forKey:@"value"];
    [dic setValue:self.scoreArray[index][@"content"] forKey:@"content"];
    [self.scoreArray replaceObjectAtIndex:index withObject:dic];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:index inSection:1], nil] withRowAnimation:UITableViewRowAnimationNone];
}
#pragma mark - 发给孩子
- (void)sendAdviceToParents{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    
//    NSMutableArray *scoreArray = [[NSMutableArray alloc]init];
//    for (int i = 0; i < self.scoreArray.count; i++) {
//        NSMutableDictionary *muDic = [[NSMutableDictionary alloc]init];
//        [muDic setValue:self.scoreArray[i][@"value"] forKey:@"value"];
//        [muDic setValue:self.scoreArray[i][@"content"] forKey:@"content"];
//        [scoreArray addObject:muDic];
//    }
    NSData *dataFriends = [NSJSONSerialization dataWithJSONObject:self.scoreArray options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:dataFriends encoding:NSUTF8StringEncoding];
    [dic setValue:jsonString forKey:@"value"];    //评分 "{"key":"classId 科目值","score":"分数"}"
    
    [dic setValue:[NSString stringWithFormat:@"%@-%@",[self.titleStr substringToIndex:4],[self.titleStr substringWithRange:NSMakeRange(5,2)]] forKey:@"date"];  //yyyy-MM
    [dic setValue:self.releaseContentTextView.text forKey:@"context"];   //动态的内容详情
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [dic setValue:self.child_id forKey:@"child_id"];
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppInteractionAddParentSay] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            [Global showActivityWithView:self.view withText:@"发送成功"];
            if (self.sendAdviceToParentsBlock) {
                self.sendAdviceToParentsBlock();
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
